<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/6
 * Time: 9:27
 */

namespace app\api\controller\user;

use app\api\controller\HomeCommon;
use app\api\model\Area;
use app\api\model\UserAddress;
use app\lib\exception\FailedException;
use app\Validate\AddressValidate;
use app\Validate\IdMustPositive;
use app\Validate\PageMustValidate;
use think\Db;
use think\Request;


class Address extends HomeCommon
{
    /**
     * 用户默认地址
     * @param Request $request
     */
    public function UserDefault(Request $request){
        $post = $request->post();
        $default_address = (new UserAddress())->findOne([['user_id','eq',$post['user_id']],['is_default','eq',1]]);
        if($default_address){
            return writeJson(200,['is_default'=>1,'default_address'=>$default_address]);
        }
        return writeJson(200,['is_default'=>0,'default_address'=>$default_address]);
    }
    /**
     * 用户地址列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \app\lib\exception\ParameterException
     */
    public function UserAddressList(Request $request){
        (new PageMustValidate())->goCheck();
        $post = $request->post();
        $where = [];
        $where[] = ['user_id','eq',$post['user_id']];
        $where['page'] = $post['page'];
        $list = (new UserAddress())->wechatListTotal($where,"*","is_default desc,id desc",$post['page_number']);
        return writeJson(200,$list);
    }

    /**
     * 地址新增
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function UserAddressAdd(Request $request){
        (new AddressValidate())->goCheck();
        $post = $request->post();
        if(!isMobilephone($post['contact_number'])){
            throw new FailedException(['msg'=>'电话号格式错误']);
        }
        $new = [
            'user_id'=>$post['user_id'],
            'linkman'=>$post['linkman'],
            'contact_number'=>$post['contact_number'],
            'province_id'   =>$post['province_id'],
            'city_id'       =>$post['city_id'],
            'area_id'       =>$post['area_id'],
            'province'      =>$post['province'],
            'city'        =>$post['city'],
            'area'        =>$post['area'],
            'address'     =>$post['address'],
            'is_default'  =>$post['is_default']
        ];
        Db::startTrans();
        if($new['is_default']==1){
            $res = (new UserAddress())->fieldUpdate("is_default",0,"",[['user_id','eq',$post['user_id']]]);
            if(!$res){
                Db::rollback();
                throw new FailedException(['msg'=>'地址编辑失败']);
            }
        }
        $res = (new UserAddress())->dataUpdate($new);
        if(!$res){
            Db::rollback();
            throw new FailedException(['msg'=>'地址新增失败']);
        }
        Db::commit();
        return writeJson(200,[],'地址新增成功');
    }

    /**
     * 地址编辑
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function UserAddressEdit(Request $request){
        (new AddressValidate())->goCheck();
        (new IdMustPositive())->goCheck();
        $post = $request->post();
        if(!isMobilephone($post['contact_number'])){
            throw new FailedException(['msg'=>'电话号格式错误']);
        }
        $new = [
            'id'     =>$post['id'],
            'user_id'=>$post['user_id'],
            'linkman'=>$post['linkman'],
            'contact_number'=>$post['contact_number'],
            'province_id'   =>$post['province_id'],
            'city_id'       =>$post['city_id'],
            'area_id'       =>$post['area_id'],
            'province'      =>$post['province'],
            'city'        =>$post['city'],
            'area'        =>$post['area'],
            'address'     =>$post['address'],
            'is_default'  =>$post['is_default']
        ];
        Db::startTrans();
        if($new['is_default']==1){
            $res = (new UserAddress())->fieldUpdate("is_default",0,"",[['user_id','eq',$post['user_id']]]);
            if(!$res){
                Db::rollback();
                throw new FailedException(['msg'=>'地址编辑失败']);
            }
        }
        $res = (new UserAddress())->dataUpdate($new);
        if(!$res){
            Db::rollback();
            throw new FailedException(['msg'=>'地址编辑失败']);
        }
        Db::commit();
        return writeJson(200,[],'地址编辑成功');
    }
    /**
     * 地址删除
     * @param Request $request
     * @return \think\response\Json
     * @throws FailedException
     * @throws \app\lib\exception\ParameterException
     */
    public function UserAddressDelete(Request $request){
        $post = $request->post();
        if(!isset($post['id'])){
            throw new FailedException(['msg'=>'请选择需要删除的地址']);
        }
        $res = (new UserAddress())->fieldUpdate('delete_time',time(),$post['id']);
        if(!$res){
            throw new FailedException(['msg'=>'地址删除失败']);
        }
        return writeJson(200,[],'地址删除成功');
    }



}